package com.hnkywl.sts.service.bg;

import com.hnkywl.sts.dao.base.BmDao;
import com.hnkywl.sts.dao.base.SjzdsyDao;
import com.hnkywl.sts.dao.base.SjzdzDao;
import com.hnkywl.sts.dao.bg.BgWzbmDao;
import com.hnkywl.sts.dao.organ.BmfwDao;
import com.hnkywl.sts.dao.sys.*;
import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.entity.organ.Bmfw;
import com.hnkywl.sts.entity.sys.*;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.webservice.bg.request.BgWzbm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-25
 * Time: 下午2:42
 * To change this template use File | Settings | File Templates.
 */
@Service
public class BgWzbmService {
    private BgWzbmDao bgWzbmDao;
    private GgDao ggDao;
    private PmDao pmDao;
    private RjwzbmDao rjwzbmDao;
    private YjwzbmDao yjwzbmDao;
    private WzbmDao wzbmDao;
    private BmDao bmDao;
    private BmfwDao bmfwDao;
    private SjzdzDao sjzdzDao;
    private SjzdsyDao sjzdsyDao;

    /**
     * 接口调用，保存物资编码
     */
    public List<Map<String, Object>> interfaceSave(List<Map<String, Object>> bgWzbmList) throws Exception {
        List<Map<String, Object>> rtList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> item : bgWzbmList) {
            //  Map<String, Object> rtItem = new HashMap<String, Object>();
            //   rtItem.put("code", item.get("code"));
//            try {
            save(item);
//                rtItem.put("code", "0");
//                rtItem.put("msg", "创建物资已成功！");
//            } catch (Exception e) {
//                rtItem.put("code", "1");
//                rtItem.put("msg", e.getMessage());
//            }
            //    rtList.add(rtItem);
        }
        return rtList;
    }

    @Transactional
    public void save(Map<String, Object> item) throws Exception {
        addGg(item, addPm(item, addRjwzbm(item, addYjwzbm(item))));
        addCd((String) item.get("field"), (String) item.get("fieldCode"));
    }

    public Yjwzbm addYjwzbm(Map item) {
        Yjwzbm y = yjwzbmDao.findByCode((String) item.get("l1Code"));
        if (null != y) {
            return y;
        }
        Yjwzbm yjwzbm = new Yjwzbm();
        Bm bm = new Bm();
        bm.setZt("1");
        bm.setBmz((String) item.get("l1Code"));
        bm.setBmlxid(1);
        bm.setSCSJ(new Date());
        bm.setSYSJ(new Date());
        yjwzbm.setCode((String) item.get("l1Code"));
        yjwzbm.setName((String) item.get("l1Name"));
        yjwzbm.setDateCreated(new Date());
        yjwzbm.setLastUpdated(yjwzbm.getDateCreated());
        yjwzbmDao.save(yjwzbm);
        bmDao.save(bm);
        addBmfw(yjwzbm.getId(), 1);
        return yjwzbm;
    }

    public Rjwzbm addRjwzbm(Map item, Yjwzbm y) {
        Rjwzbm r = rjwzbmDao.findByCode((String) item.get("l2Code"));
        if (null != r) {
            return r;
        }
        Rjwzbm rjwzbm = new Rjwzbm();
        Bm bm = new Bm();
        bm.setZt("1");
        bm.setBmz((String) item.get("l2Code"));
        bm.setBmlxid(2);
        bm.setSCSJ(new Date());
        bm.setSYSJ(new Date());
        rjwzbm.setCode((String) item.get("l2Code"));
        rjwzbm.setName((String) item.get("l2Name"));
        rjwzbm.setDateCreated(new Date());
        rjwzbm.setLastUpdated(rjwzbm.getDateCreated());
        rjwzbm.setYjwzbm(y);
        rjwzbmDao.save(rjwzbm);
        bmDao.save(bm);
        addBmfw(rjwzbm.getId(), 2);
        return rjwzbm;
    }

    public Pm addPm(Map item, Rjwzbm r) {
        Pm p = pmDao.findByCode((String) item.get("l3Code"));
        if (null != p) {
            return p;
        }
        Pm pm = new Pm();
        Bm bm = new Bm();
        bm.setZt("1");
        bm.setBmz((String) item.get("l3Code"));
        bm.setBmlxid(3);
        bm.setSCSJ(new Date());
        bm.setSYSJ(new Date());
        pm.setCode((String) item.get("l3Code"));
        pm.setName((String) item.get("l3Name"));
        pm.setDateCreated(new Date());
        pm.setLastUpdated(pm.getDateCreated());
        pm.setRjwzbm(r);
        pmDao.save(pm);
        bmDao.save(bm);
        addBmfw(pm.getId(), 3);
        return pm;
    }

    public void addGg(Map item, Pm p) throws Exception {
        Gg g = ggDao.findByCode((String) item.get("code"));
        if (g != null) {
            throw new RuntimeException("已经存在此物资信息");
        }
        Gg gg = new Gg();
        Bm bm = new Bm();
        bm.setZt("1");
        bm.setBmz((String) item.get("code"));
        bm.setBmlxid(4);
        bm.setSCSJ(new Date());
        bm.setSYSJ(new Date());
        gg.setCode((String) item.get("code"));
        gg.setName((String) item.get("name"));
        gg.setCz((String) item.get("materialQuality"));
        gg.setCzCode((String) item.get("materialQualityCode"));
        gg.setGgxh((String) item.get("standardModel"));
        gg.setGgjc((String) item.get("standardModel"));
//        gg.setGgjc((String) item.get("shortStandardName"));
        gg.setPmCode((String) item.get("nameCode"));
        gg.setGgCode((String) item.get("standardModelCode"));
        gg.setDw((String) item.get("unitName"));
        gg.setDateCreated(new Date());
        gg.setLastUpdated(new Date());
        gg.setPm(p);
        bmDao.save(bm);
        ggDao.save(gg);
    }

    public void addCd(String cd, String bm) {
        Sjzdz s = sjzdzDao.findUniqueByProperty("bm", bm);
        if (s == null) {
            Sjzdz sjzdz = new Sjzdz();
            sjzdz.setBm(bm);
            sjzdz.setZ(cd);
            sjzdz.setSjzdsy(sjzdsyDao.findUniqueByProperty("bm", "cd"));
            sjzdz.setDateCreated(new Date());
            sjzdz.setLastUpdated(new Date());
            sjzdzDao.save(sjzdz);
        }
    }

    public void addBmfw(Long wzbmId, int type) {
        if(wzbmId!=null) {
            List wzbmList = this.bmfwDao.find("from Bmfw where wzbmId=?", wzbmId);
            if (wzbmList.size() <= 0) {
                Bmfw bmfw = new Bmfw();
                bmfw.setGsId(11903l);
                bmfw.setType(type);
                bmfw.setWzbmId(wzbmId);
                this.bmfwDao.save(bmfw);
            }
        }
    }

    @Transactional
    public void save(BgWzbm bgWzbm) {
        bgWzbmDao.save(bgWzbm);
    }

//    @Transactional
//    public void addYjwzbm(BgWzbm bgWzbm) throws Exception {
//        Bm b = bmDao.findByCodeId(bgWzbm.getCodeid(), "yjwzbm");
//        if (null != b) {
//            throw new RuntimeException("该物资在系统中已存在codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Yjwzbm yjwzbm = new Yjwzbm();
//        Bm bm = bmDao.getYjwzbm();
//        bm.setZt("1");
//        bm.setBmylz2(bgWzbm.getCode());
//        yjwzbm.setCode(bm.getBmz());
//        bm.setBmylz1(bgWzbm.getCodeid());
//        yjwzbm.setJc(bgWzbm.getShortname());
//        yjwzbm.setName(bgWzbm.getName());
//        yjwzbm.setDateCreated(new Date());
//        yjwzbm.setLastUpdated(yjwzbm.getDateCreated());
//        yjwzbmDao.save(yjwzbm);
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void updateYjwzbm(BgWzbm bgWzbm) throws Exception {
//        Bm bm = bmDao.findByCodeId(bgWzbm.getCodeid(), "yjwzbm");
//        if (null == bm) {
//            throw new RuntimeException("修改的物资在系统未找到codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Yjwzbm yjwzbm = yjwzbmDao.findByCode(bm.getBmz());
//        bm.setBmylz2(bgWzbm.getCode()); //设置code
//        yjwzbm.setJc(bgWzbm.getShortname());
//        yjwzbm.setName(bgWzbm.getName());
//        yjwzbm.setLastUpdated(new Date());
//        yjwzbmDao.save(yjwzbm);
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void addRjwzbm(BgWzbm bgWzbm) throws Exception {
//        Bm b = bmDao.findByCodeId(bgWzbm.getCodeid(), "rjwzbm");
//        if (null != b) {
//            throw new RuntimeException("该物资在系统中已存在codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Rjwzbm rjwzbm = new Rjwzbm();
//        Object[] objects = wzbmDao.findByCode(bgWzbm.getParentcode(), "yjwzbm");
//        if (objects == null) {
//            throw new RuntimeException("上级物资未找到parentCode:" + bgWzbm.getParentcode());
//        }
//        Yjwzbm y = new Yjwzbm();
//        y.setId(((BigDecimal) objects[0]).longValue());
//        rjwzbm.setYjwzbm(y);
//        Bm bm = bmDao.getEjwzbm((String) objects[1]);
//        bm.setBmylz2(bgWzbm.getCode());
//        bm.setZt("1");
//        rjwzbm.setCode(bm.getBmz());
//        bm.setBmylz1(bgWzbm.getCodeid());
//        rjwzbm.setJc(bgWzbm.getShortname());
//        rjwzbm.setName(bgWzbm.getName());
//        rjwzbm.setDateCreated(new Date());
//        rjwzbm.setLastUpdated(rjwzbm.getDateCreated());
//        rjwzbmDao.save(rjwzbm);
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void updateRjwzbm(BgWzbm bgWzbm) throws Exception {
//        Bm bm = bmDao.findByCodeId(bgWzbm.getCodeid(), "rjwzbm");
//        if (null == bm) {
//            throw new RuntimeException("修改的物资在系统未找到codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Rjwzbm rjwzbm = rjwzbmDao.findByCode(bm.getBmz());
//        bm.setBmylz2(bgWzbm.getCode());
//        rjwzbm.setJc(bgWzbm.getShortname());
//        rjwzbm.setName(bgWzbm.getName());
//        rjwzbm.setLastUpdated(new Date());
//        rjwzbmDao.save(rjwzbm);
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void addPm(BgWzbm bgWzbm) throws Exception {
//        Bm b = bmDao.findByCodeId(bgWzbm.getCodeid(), "pm");
//        if (null != b) {
//            throw new RuntimeException("该物资在系统中已存在codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Pm pm = new Pm();
//        Object[] objects = wzbmDao.findByCode(bgWzbm.getParentcode(), "rjwzbm");
//        if (objects == null) {
//            throw new RuntimeException("上级物资未找到parentCode:" + bgWzbm.getParentcode());
//        }
//        Rjwzbm r = new Rjwzbm();
//        r.setId(((BigDecimal) objects[0]).longValue());
//        pm.setRjwzbm(r);
//        Bm bm = bmDao.getPmwzbm((String) objects[1]);
//        bm.setBmylz2(bgWzbm.getCode());
//        bm.setZt("1");
//        pm.setCode(bm.getBmz());
//        bm.setBmylz1(bgWzbm.getCodeid());
//        pm.setGysmc(bgWzbm.getProvider());
//        pm.setJc(bgWzbm.getShortname());
//        pm.setName(bgWzbm.getName());
//        pm.setDateCreated(new Date());
//        pm.setLastUpdated(pm.getDateCreated());
//        pmDao.save(pm);
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void updatePm(BgWzbm bgWzbm) throws Exception {
//        Bm bm = bmDao.findByCodeId(bgWzbm.getCodeid(), "pm");
//        if (null == bm) {
//            throw new RuntimeException("修改的物资在系统未找到codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Pm pm = pmDao.findByCode(bm.getBmz());
//        bm.setBmylz2(bgWzbm.getCode());
//        bm.setBmylz1(bgWzbm.getCodeid());
//        pm.setGysmc(bgWzbm.getProvider());
//        pm.setJc(bgWzbm.getShortname());
//        pm.setName(bgWzbm.getName());
//        pm.setLastUpdated(new Date());
//        pmDao.save(pm);
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void addGg(BgWzbm bgWzbm) throws Exception {
//        Bm b = bmDao.findByCodeId(bgWzbm.getCodeid(), "gg");
//        if (null != b) {
//            throw new RuntimeException("该物资在系统中已存在codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Gg gg = new Gg();
//        Object[] objects = wzbmDao.findByCode(bgWzbm.getParentcode(), "pm");
//        if (objects == null) {
//            throw new RuntimeException("上级物资未找到parentCode:" + bgWzbm.getParentcode());
//        }
//        Pm p = new Pm();
//        p.setId(((BigDecimal) objects[0]).longValue());
//        gg.setPm(p);
//        Bm bm = bmDao.getGgwzbm((String) objects[1]);
//        bm.setBmylz2(bgWzbm.getCode());
//        bm.setZt("1");
//        gg.setCode(bm.getBmz());
//        bm.setBmylz1(bgWzbm.getCodeid());
//        gg.setGgjc(bgWzbm.getShortname());
//        gg.setName(bgWzbm.getName());
//        gg.setDateCreated(new Date());
//        gg.setOldcode(bgWzbm.getCode());
//        gg.setLastUpdated(gg.getDateCreated());
//        gg.setCz(bgWzbm.getMaterial());
//        gg.setDw(bgWzbm.getUnit());
//        gg.setGgxh(bgWzbm.getSpec());
//        gg.setTh(bgWzbm.getPicno());
//        ggDao.save(gg);
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void updateGg(BgWzbm bgWzbm) throws Exception {
//        //先禁用
//        Bm b = bmDao.findByCodeId(bgWzbm.getCodeid(), "gg");
//        if (null == b) {
//            throw new RuntimeException("修改的物资在系统未找到codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Gg g = ggDao.findByCode(b.getBmz());
//        if (!bgWzbmDao.existGg(g.getId())) {
//            b.setZt("2");
//            b.setZXSJ(new Date());
//        } else {
//            b.setZt("3");
//            b.setFCSJ(new Date());
//        }
//        bmDao.save(b);
//        //后新增
//        Gg gg = new Gg();
//        Object[] objects = wzbmDao.findByCode(bgWzbm.getParentcode(), "pm");
//        if (objects == null) {
//            throw new RuntimeException("上级物资未找到parentCode:" + bgWzbm.getParentcode());
//        }
//        Pm p = new Pm();
//        p.setId(((BigDecimal) objects[0]).longValue());
//        gg.setPm(p);
//        Bm bm = bmDao.getGgwzbm((String) objects[1]);
//        bm.setBmylz2(bgWzbm.getCode());
//        bm.setZt("1");
//        gg.setCode(bm.getBmz());
//        bm.setBmylz1(bgWzbm.getCodeid());
//        gg.setGgjc(bgWzbm.getShortname());
//        gg.setName(bgWzbm.getName());
//        gg.setDateCreated(new Date());
//        gg.setOldcode(bgWzbm.getCode());
//        gg.setLastUpdated(gg.getDateCreated());
//        gg.setCz(bgWzbm.getMaterial());
//        gg.setDw(bgWzbm.getUnit());
//        gg.setGgxh(bgWzbm.getSpec());
//        gg.setTh(bgWzbm.getPicno());
//        ggDao.save(gg);
//        bmDao.save(bm);
//    }
//
//    //==========废除物资编码===============
//    @Transactional
//    public void destroyYjwzbm(BgWzbm bgWzbm) throws Exception {
//        Bm bm = bmDao.findByCodeId(bgWzbm.getCodeid(), "yjwzbm");
//        if (null == bm) {
//            throw new RuntimeException("禁用的物资在系统未找到codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        bm.setZt("3");
//        bm.setFCSJ(new Date());
////            Yjwzbm yjwzbm = yjwzbmDao.findByCode(bm.getBmz());
//        if (yjwzbmDao.existRjwzbm(bm.getBmz())) {
//            throw new RuntimeException("该物资存在下级代码，禁止删除codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void destroyRjwzbm(BgWzbm bgWzbm) throws Exception {
//        Bm bm = bmDao.findByCodeId(bgWzbm.getCodeid(), "rjwzbm");
//        if (null == bm) {
//            throw new RuntimeException("禁用的物资在系统未找到codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        bm.setZt("3");
//        bm.setFCSJ(new Date());
////            Rjwzbm rjwzbm = rjwzbmDao.findByCode(bm.getBmz());
//        if (rjwzbmDao.existPm(bm.getBmz())) {
//            throw new RuntimeException("该物资存在下级代码，禁止删除codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void destroyPm(BgWzbm bgWzbm) throws Exception {
//        Bm bm = bmDao.findByCodeId(bgWzbm.getCodeid(), "pm");
//        if (null == bm) {
//            throw new RuntimeException("禁用的物资在系统未找到codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        bm.setZt("3");
//        bm.setFCSJ(new Date());
////            Pm pm = pmDao.findByCode(bm.getBmz());
//        if (pmDao.existGg(bm.getBmz()))
//            throw new RuntimeException("该物资存在下级代码，禁止删除codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        bmDao.save(bm);
//    }
//
//    @Transactional
//    public void destroyGg(BgWzbm bgWzbm) throws Exception {
//        Bm bm = bmDao.findByCodeId(bgWzbm.getCodeid(), "gg");
//        if (null == bm) {
//            throw new RuntimeException("禁用的物资在系统未找到codeId:" + bgWzbm.getCodeid() + "code:" + bgWzbm.getCode());
//        }
//        Gg gg = ggDao.findByCode(bm.getBmz());
//        if (!bgWzbmDao.existGg(gg.getId())) {
//            bm.setZt("2");
//            bm.setZXSJ(new Date());
//        } else {
//            bm.setZt("3");
//            bm.setFCSJ(new Date());
//        }
//        bmDao.save(bm);
//    }


    //==========setter==========
    @Autowired
    public void setBgWzbmDao(BgWzbmDao bgWzbmDao) {
        this.bgWzbmDao = bgWzbmDao;
    }

    @Autowired
    public void setGgDao(GgDao ggDao) {
        this.ggDao = ggDao;
    }

    @Autowired
    public void setPmDao(PmDao pmDao) {
        this.pmDao = pmDao;
    }

    @Autowired
    public void setRjwzbmDao(RjwzbmDao rjwzbmDao) {
        this.rjwzbmDao = rjwzbmDao;
    }

    @Autowired
    public void setYjwzbmDao(YjwzbmDao yjwzbmDao) {
        this.yjwzbmDao = yjwzbmDao;
    }

    @Autowired
    public void setWzbmDao(WzbmDao wzbmDao) {
        this.wzbmDao = wzbmDao;
    }

    @Autowired
    public void setBmDao(BmDao bmDao) {
        this.bmDao = bmDao;
    }

    @Autowired
    public void setSjzdzDao(SjzdzDao sjzdzDao) {
        this.sjzdzDao = sjzdzDao;
    }

    @Autowired
    public void setSjzdsyDao(SjzdsyDao sjzdsyDao) {
        this.sjzdsyDao = sjzdsyDao;
    }

    @Autowired
    public void setBmfwDao(BmfwDao bmfwDao) {
        this.bmfwDao = bmfwDao;
    }
}
